/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Schnittstellen.Audio;

import Schnittstellen.Position;
import java.io.File;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.DataLine;

/**
 *
 * @author Geez
 */
public class AudioFile extends Sound {

    private String name;
    private static boolean playing = false;

    public AudioFile(String name) {
        this.name = name;

    }

    @Override
    public boolean playSound() {
        playAudio(this);
        return true;
    }

    @Override
    public boolean playSound(Position p) {
        playAudio(this);
        return true;
    }
    private static Clip clip;

    public static void playAudio(Sound sound) {
        if (playing) {
            clip.stop();
            playing = false;
        } else {
            try {
                AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(new File("assets/Sounds/" + sound.getEditorName()));
                AudioFormat af = audioInputStream.getFormat();
                int size = (int) (af.getFrameSize() * audioInputStream.getFrameLength());
                byte[] audio = new byte[size];
                DataLine.Info info = new DataLine.Info(Clip.class, af, size);
                audioInputStream.read(audio,
                        0, size);

                // for(int i=0; i < 32; i++) {
                clip = (Clip) AudioSystem.getLine(info);

                clip.open(af, audio,
                        0, size);
                clip.start();
                playing = true;
                // }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
